1   /*
2    * Copyright (C) 2010 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.primitives;
18  
19  import com.google.caliper.BeforeExperiment;
20  import com.google.caliper.Benchmark;
21  import com.google.caliper.Param;
22  
23  import java.util.Arrays;
24  import java.util.Comparator;
25  import java.util.Random;
26  
27  /**
28   * Microbenchmark for {@link UnsignedBytes}.
29   *
30   * @author Hiroshi Yamauchi
31   */
32  public class UnsignedBytesBenchmark {
33  
34    private byte[] ba1;
35    private byte[] ba2;
36    private byte[] ba3;
37    private byte[] ba4;
38    private Comparator<byte[]> javaImpl;
39    private Comparator<byte[]> unsafeImpl;
40  
41    // 4, 8, 64, 1K, 1M, 1M (unaligned), 64M, 64M (unaligned)
42    //@Param({"4", "8", "64", "1024", "1048576", "1048577", "6710884", "6710883"})
43    @Param({"4", "8", "64", "1024" })
44    private int length;
45  
46    @BeforeExperiment
47    void setUp() throws Exception {
48      Random r = new Random();
49      ba1 = new byte[length];
50      r.nextBytes(ba1);
51      ba2 = Arrays.copyOf(ba1, ba1.length);
52      // Differ at the last element
53      ba3 = Arrays.copyOf(ba1, ba1.length);
54      ba4 = Arrays.copyOf(ba1, ba1.length);
55      ba3[ba1.length - 1] = (byte) 43;
56      ba4[ba1.length - 1] = (byte) 42;
57  
58      javaImpl = UnsignedBytes.lexicographicalComparatorJavaImpl();
59      unsafeImpl =
60          UnsignedBytes.LexicographicalComparatorHolder.UnsafeComparator.INSTANCE;
61    }
62  
63    @Benchmark void longEqualJava(int reps) {
64      for (int i = 0; i < reps; ++i) {
65        if (javaImpl.compare(ba1, ba2) != 0) {
66          throw new Error(); // deoptimization
67        }
68      }
69    }
70  
71    @Benchmark void longEqualUnsafe(int reps) {
72      for (int i = 0; i < reps; ++i) {
73        if (unsafeImpl.compare(ba1, ba2) != 0) {
74          throw new Error(); // deoptimization
75        }
76      }
77    }
78  
79    @Benchmark void diffLastJava(int reps) {
80      for (int i = 0; i < reps; ++i) {
81        if (javaImpl.compare(ba3, ba4) == 0) {
82          throw new Error(); // deoptimization
83        }
84      }
85    }
86  
87    @Benchmark void diffLastUnsafe(int reps) {
88      for (int i = 0; i < reps; ++i) {
89        if (unsafeImpl.compare(ba3, ba4) == 0) {
90          throw new Error(); // deoptimization
91        }
92      }
93    }
94  
95    /*
96    try {
97      UnsignedBytesBenchmark bench = new UnsignedBytesBenchmark();
98      bench.length = 1024;
99      bench.setUp();
100     bench.timeUnsafe(100000);
101   } catch (Exception e) {
102   }*/
103 }